/**********************************************
** EPOC Example: How to use images with EPOC **
***********************************************
* Author: Jonas Svegland                     **
* Date: 2008, 5 February					 **
***********************************************/

#include <image.h>
#include <math.h>

#include "game.h"
#include "application.h"

using namespace EPOC;

// It's here that you can set members to their default values
Game::Game(Application *pApplication) : GameManager(pApplication)
{ 
	// resetting members
	m_fTimer = 0.0f;
	m_vZombiePos = Vector2D(0.0f, 0.0f);
	m_pZombie = NULL;
	m_pZombieShadow = NULL;
}

// It's here that you can delete, releas or remove members
Game::~Game() 
{ 
	// no need to remove created images, their automaticly removed when the application exits
	// (but you could if you want to) just use: GetApplication()->GetGraphicsManager()->DestroyImage(...)
	if(m_pZombie)
		GetApplication()->GetGraphicsManager()->DestroyImage(m_pZombie);

	if(m_pZombieShadow)
		GetApplication()->GetGraphicsManager()->DestroyImage(m_pZombieShadow);
}


// It's in this method that you should create all your application data
bool Game::Initialize()
{
	// Get the graphicsmanager pointer from EPOC::Application
	GraphicsManager *pGraphicsManager = GetApplication()->GetGraphicsManager();
	if(pGraphicsManager == NULL)
		return false;

	// create a background image, set it use pivot as upper left of image 
	// instead of the center of the image (the default pivot)
	Image *pBackground = pGraphicsManager->CreateImage("data/graphics/background.png");
	pBackground->SetPivotCentered(true);
	pBackground->SetPos(320.0f, 240.0f);
	pBackground->SetCullingEnabled(false);

	m_vZombiePos = Vector2D(320.0f, 340.0f);
	// create a zombie image, set it to use alpha transparency and
	// set it to z order 2 (0 or EP_ZORDER_MIN equals furthest away 
	// and 4294967295 or EP_ZORDER_MAX is closest to the viewer)
	m_pZombie = pGraphicsManager->CreateImage("data/graphics/zombie.png");
	m_pZombie->SetAlphaEnabled(true);
	m_pZombie->SetZOrder(2);
	m_pZombie->SetPos(m_vZombiePos);
	
	
	// create a zombie shadow image, set it to use alpha transparency
	// and set it to z order 1, (behind the zombie image)
	m_pZombieShadow = pGraphicsManager->CreateImage("data/graphics/zombie_shadow.png");
	m_pZombieShadow->SetAlphaEnabled(true);
	m_pZombieShadow->SetPos(m_vZombiePos.m_fX, m_vZombiePos.m_fY + 110);
	m_pZombieShadow->SetZOrder(1);
	

	return true;
}

bool Game::Update(float fTimeDelta)
{
	// if m_pImage and m_pImage2 exists (the zomie image and the zombie shadow image)
	if(m_pZombie && m_pZombieShadow) 
	{
		// increase timer used by sin function
		m_fTimer += fTimeDelta;

		// calculate offset positions with sin function and timer
		Vector2D vPosOffset(sinf(m_fTimer * 0.001f) * 200.0f, sinf(m_fTimer * 0.05f) * 5.0f);
		
		// calculate new position of zombie image, but keep the original positions
		Vector2D vNewPos = m_vZombiePos + vPosOffset; 
		
		// update the position fo the zombie image and the shadow image
		m_pZombie->SetPos(vNewPos);
		m_pZombieShadow->SetPosX(vNewPos.m_fX);
	}
	
	return true;
}
